Skip to content

Image Editor: Add mapImageEdit to apply an edit's parts in order - #86704

Merged
andrewserong merged 4 commits into
trunkfrom
add/image-editor-map-image-edit
Nov 17, 2026
Merged

andrewserong merged 4 commits into
trunkfrom
add/image-editor-map-image-edit

Conversation

@ramonjd

@ramonjd ramonjd commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

What?

Part of:

mapImageEdit( edit, handlers ) turns each part of an ImageEdit into the consumer's own format, in the order the parts apply: flip, then rotation, then crop. Parts the edit does not have are skipped. The media editor's save now uses it to build the REST modifiers:

const modifiers = mapImageEdit< Modifier >( edit, {
	flip: ( flip ) => ( { type: 'flip', args: { flip } } ),
	rotation: ( angle ) => ( { type: 'rotate', args: { angle } } ),
	crop: ( { x, y, width, height } ) => ( {
		type: 'crop',
		args: { left: x * 100, top: y * 100, width: width * 100, height: height * 100 },
	} ),
} );

Why?

Follow-up to #86630, raised in review there. ImageEdit is a record, so it does not say what order its parts apply in, and the order is not optional: rotation is measured after the flip, and crop is a fraction of the image after both. Before this, every consumer that sends the edit to something that applies steps one at a time had to know that and write three ifs in the right order. The media editor did, and so did the recipe in the docs. Now the package owns the order.

Testing Instructions

  1. Open an image in the media editor, flip it, rotate it 90° and crop it, then save.
  2. Check the saved image matches what the canvas showed.

@ramonjd ramonjd added [Type] New API New API to be used by plugin developers or package users. [Package] Image Editor Related to packages/image-editor labels Nov 12, 2026
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: +36 B (0%)

Total Size: 8.66 MB

📦 View Changed
Filename Size Change
build/scripts/editor/index.min.js 571 kB +36 B (+0.01%)

5344811 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric e6c4766 trunk % Change
timeToFirstByte 55 ms +3.64% -3.09% 57.05 ms +2.54% -6.57% -3.59%
largestContentfulPaint 86 ms +6.98% -2.33% 88 ms +9.09% -2.27% -2.27%
lcpMinusTtfb 29.1 ms +16.15% -5.67% 30.6 ms +20.1% -5.23% -4.9%
wpBeforeTemplate 29.05 ms +7.02% -2.79% 29.67 ms +10.41% -2.83% -2.09%
wpTemplate 21.76 ms +3.49% -1.56% 21.48 ms +11.17% -4.7% 1.3%
wpTotal 51.15 ms +3.99% -3.05% 53.25 ms +2.63% -6.72% -3.94%
wpMemoryUsage 7.75 MB +0% -0% 7.71 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric e6c4766 trunk % Change
timeToFirstByte 46.15 ms +3.36% -2.82% 46.2 ms +6.49% -2.38% -0.11%
largestContentfulPaint 82 ms +2.44% -2.44% 80 ms +5% -0% 2.5%
lcpMinusTtfb 35.45 ms +4.94% -3.67% 34.7 ms +1.44% -2.31% 2.16%
wpBeforeTemplate 26.28 ms +4.68% -1.22% 26.31 ms +6.23% -0.61% -0.11%
wpTemplate 16.26 ms +6.03% -1.48% 16.28 ms +5.59% -1.47% -0.12%
wpTotal 43.03 ms +3.79% -2.67% 43.24 ms +6.29% -2.43% -0.49%
wpMemoryUsage 6.44 MB +0% -0% 6.40 MB +0% -0% 0.56%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric e6c4766 trunk % Change
mediaProcessingJpeg 400.94 ms +0.42% -0.37% 399.43 ms +0.42% -0.22% 0.38%
mediaProcessingAvif 6129.75 ms +0.69% -0.16% 6132.76 ms +0.12% -0.24% -0.05%
mediaProcessingJpegToAvif 4245.18 ms +0.24% -0.26% 4220.03 ms +0.41% -0.14% 0.6%

media-upload

Metric e6c4766 trunk % Change
jpegUploadProcessing 1986.51 ms +0.33% -23.91% 1509 ms +31.12% -0.68% 31.64%
pngUploadProcessing 272.82 ms +3.1% -2.1% 270.45 ms +3.36% -3.31% 0.88%
largeJpegUploadProcessing 1517.85 ms +29.62% -1.2% 1968.07 ms +0.45% -22.88% -22.88%
multipleImageUploadProcessing 2070.8 ms +0.87% -0.8% 2060.63 ms +1.96% -0.44% 0.49%

post-editor

Metric e6c4766 trunk % Change
serverResponse 440.65 ms +10.54% -1.97% 483.1 ms +1.26% -5.48% -8.79%
firstPaint 445.43 ms +11.36% -10.7% 400.31 ms +6.94% -4.33% 11.27%
domContentLoaded 807.28 ms +10.28% -2.18% 804.6 ms +1.92% -0.97% 0.33%
loaded 808.48 ms +10.23% -2.2% 805.77 ms +1.92% -1.01% 0.34%
firstContentfulPaint 457.63 ms +16.05% -2.6% 451.06 ms +2.22% -1.16% 1.46%
firstBlock 2196.97 ms +0.68% -0.29% 2164.52 ms +0.58% -1.47% 1.5%
type 17.2 ms +2.97% -3.49% 17.03 ms +2.76% -6.05% 1%
typeWithoutInspector 17.11 ms +6.84% -9.59% 16.21 ms +2.16% -0.43% 5.55%
typeWithTopToolbar 22.96 ms +7.36% -9.89% 22.97 ms +3.35% -12.15% -0.04%
typeContainer 8.47 ms +13.46% -10.98% 8.62 ms +14.04% -12.53% -1.74%
focus 122.36 ms +7.71% -7.92% 113.78 ms +6.85% -1.02% 7.54%
firstFocus 263.93 ms +0% -0% 265.22 ms +0% -0% -0.49%
selectAll 446.29 ms +4.64% -8.41% 391.53 ms +0.58% -3.23% 13.99%
undo 28.52 ms +2.81% -7.61% 28.46 ms +4.29% -4.81% 0.21%
listViewOpen 81.03 ms +22.66% -4.85% 75.64 ms +5.96% -3.31% 7.13%
inserterOpen 42.04 ms +3.66% -2.62% 39.91 ms +12.33% -7.62% 5.34%
inserterHover 11.31 ms +10.61% -12.91% 11.36 ms +21.74% -11.71% -0.44%
inserterSearch 8.3 ms +1.2% -5.06% 8.09 ms +5.32% -13.35% 2.6%
loadPatterns 654.24 ms +6.1% -2.1% 627.46 ms +7.43% -5.59% 4.27%
wpTotal 430.98 ms +10.79% -2.01% 473.42 ms +1.27% -5.63% -8.96%
wpMemoryUsage 14.81 MB +0% -0% 14.77 MB +0% -0% 0.25%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric e6c4766 trunk % Change
serverResponse 391.84 ms +2.46% -6.66% 391.8 ms +0.93% -7.23% 0.01%
firstPaint 443.86 ms +6.15% -15.93% 433.84 ms +2.69% -9.83% 2.31%
domContentLoaded 803.1 ms +8.18% -1.19% 789.9 ms +1.67% -0.77% 1.67%
loaded 804.06 ms +8.15% -1.17% 790.88 ms +1.7% -0.8% 1.67%
firstContentfulPaint 458.26 ms +14.03% -2.24% 444.2 ms +4.05% -2.59% 3.17%
firstBlock 2343.21 ms +1.6% -0.94% 2313.63 ms +0.9% -1.18% 1.28%
type 17.43 ms +7.75% -9.7% 16.88 ms +7.82% -7.23% 3.26%
navigate 109.82 ms +2.64% -6.15% 108.32 ms +3.28% -5.35% 1.38%
loadPatterns 1304.11 ms +10.09% -5.35% 1293.85 ms +9.97% -18.65% 0.79%
loadPages 917.74 ms +1.92% -0.7% 921.59 ms +1.77% -1.99% -0.42%
wpTotal 382.33 ms +2.5% -6.87% 382.32 ms +0.95% -7.41% 0%
wpMemoryUsage 12.82 MB +0% -0% 12.79 MB +0% -0% 0.25%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

5344811 Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 2,
+   "offset": 2,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
does not select blocks in the canvas preview when editing from the sidebar (Site Editor v2) in /test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator: getByRole('combobox', { name: 'Search or type URL' })
Expected: focused
Timeout: 5000ms
Error: element(s) not found

Call log:
  - Expect "toBeFocused" getByRole('combobox', { name: 'Search or type URL' }) with timeout 5000ms
  - waiting for getByRole('combobox', { name: 'Search or type URL' })

    at LinkControl.useLinkControlSearch (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js:416:47)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js:179:21
should apply a named font size using the font size buttons in /test/e2e/specs/editor/various/font-size-picker.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:paragraph {"fontSize":"large"} -->
+ <!-- wp:paragraph {"fontSize":"medium"} -->
- <p class="has-large-font-size">Paragraph to be made "large"</p>
+ <p class="has-medium-font-size">Paragraph to be made "large"</p>
  <!-- /wp:paragraph -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/font-size-picker.spec.js:153:6
should arrow up into empty columns in /test/e2e/specs/editor/blocks/columns.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 0
+ Received  + 7

@@ -1,9 +1,16 @@
  Array [
    Object {
      "innerBlocks": Array [
        Object {
+         "attributes": Object {},
+         "innerBlocks": Array [],
+         "name": "core/column",
+       },
+       Object {
+         "attributes": Object {},
+         "innerBlocks": Array [],
          "name": "core/column",
        },
      ],
      "name": "core/columns",
    },

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/columns.spec.js:349:41
should give the image its context menu while selected @WebKit @Firefox in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Test timeout of 100000ms exceeded.
should reset a named font size using input field in /test/e2e/specs/editor/various/font-size-picker.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:paragraph {"fontSize":"small"} -->
+ <!-- wp:paragraph {"fontSize":"tiny"} -->
- <p class="has-small-font-size">Paragraph with font size reset using input field</p>
+ <p class="has-tiny-font-size">Paragraph with font size reset using input field</p>
  <!-- /wp:paragraph -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/font-size-picker.spec.js:216:6
should reset a named font size using the tools panel menu in /test/e2e/specs/editor/various/font-size-picker.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:paragraph {"fontSize":"medium"} -->
+ <!-- wp:paragraph {"fontSize":"small"} -->
- <p class="has-medium-font-size">Paragraph with font size reset using tools panel menu</p>
+ <p class="has-small-font-size">Paragraph with font size reset using tools panel menu</p>
  <!-- /wp:paragraph -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/font-size-picker.spec.js:178:6
should undo without broken temporary state in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:image -->
- <figure class="wp-block-image"><img alt=""/></figure>
+ <!-- wp:image {"id":21,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8889/wp-content/uploads/2026/11/be177ee7-94da-417c-91c3-f11c99596c92.png" alt="" class="wp-image-21"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49

5344811 Run

Comment thread packages/image-editor/docs/recipes.md Resolved
*/
export function mapImageEdit< T >(
edit: ImageEdit,
handlers: {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes the handlers are required. i thought about making them optional, and so mapImageEdit would just require edit and return, by default, edit's values in the right order.

But i'm not sure there’s anything useful to default to, and if a consumer passed one but not the other that could lead to a mistake in the final crop.

@ramonjd ramonjd self-assigned this Nov 12, 2026
@ramonjd
ramonjd requested a balanced review from Copilot November 12, 2026 03:17
@ramonjd
ramonjd marked this pull request as ready for review November 12, 2026 03:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

ramonjd and others added 4 commits November 17, 2026 17:30
An ImageEdit is a record, so it does not carry the order its parts
apply in: flip, then rotation, then crop. The order is not optional,
because the rotation is measured after the flip and the crop after
both. Each consumer that builds instructions for something that applies
them one at a time, such as the media REST endpoint, had to know it and
write it out by hand.

mapImageEdit calls one handler per part, in that order, skipping the
parts the edit does not have. The media editor's save uses it.
@andrewserong
andrewserong force-pushed the add/image-editor-map-image-edit branch from e78c16b to 5344811 Compare November 17, 2026 06:30

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me! I think this strikes a nice balance of ensuring the order but still allowing the consumer to define the type that the mapping conforms to.

I noticed while reviewing that passing in a type seems to be required, e.g. mapImageEdit< Modifier > so I've updated the recipe to match. An alternative might be to update mapImageEdit itself to handle returning each callback the type that the function returns (so not requiring the consumer to pass in an explicit type). But, that seemed a little brittle to me as it means the consumer could basically do whatever they want with each of the callbacks, and what you've got here means that the consumer needs to go to a little bit of extra effort to ensure they all conform.

Anyway, I'm just rabbiting on here thinking out loud. I think this is a good shape, so I'll give it the ✅ and we can get this in.

@andrewserong
andrewserong enabled auto-merge (squash) November 17, 2026 06:46
@andrewserong
andrewserong merged commit f73a34b into trunk Nov 17, 2026
88 checks passed
@andrewserong
andrewserong deleted the add/image-editor-map-image-edit branch November 17, 2026 06:50
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 17, 2026
ramonjd added a commit that referenced this pull request Nov 23, 2026
Resolve the conflicts with #86704 in the README, the changelog and the
package exports by keeping both sides. 0.1.0 is now published, so the
applyImageEdit changelog entry moves to the new Unreleased heading,
under New Features.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Image Editor Related to packages/image-editor [Type] New API New API to be used by plugin developers or package users.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants